local meta = FindMetaTable("Player")

function GM:PlayerSelectSpawn( ply )
	
	if !self.HideSpawns then
		self.HideSpawns = {}
		self.HideSpawns = table.Add( self.HideSpawns, ents.FindByClass( "info_player_combine" ) )
		self.HideSpawns = table.Add( self.HideSpawns, ents.FindByClass( "info_player_counterterrorist" ) )
		self.HideSpawns = table.Add( self.HideSpawns, ents.FindByClass( "info_player_axis" ) )
	end
	
	if !self.SeekSpawns  then
		self.SeekSpawns = {}
		self.SeekSpawns = table.Add( self.SeekSpawns, ents.FindByClass( "info_player_rebel" ) )
		self.SeekSpawns = table.Add( self.SeekSpawns, ents.FindByClass( "info_player_terrorist" ) )
		self.SeekSpawns = table.Add( self.SeekSpawns, ents.FindByClass( "info_player_allies" ) )
	end
		
	if !self.Spawns then
		self.Spawns = {}
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_combine" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_counterterrorist" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_axis" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_rebel" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_terrorist" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_allies" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "gmod_player_start" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_start" ) )
		self.Spawns = table.Add( self.Spawns, ents.FindByClass( "info_player_deathmatch" ) )
	end
	
	local team = ply:Team()

	if team == 1 && #self.HideSpawns > 0 then
		MySpawns = self.HideSpawns
	elseif team == 2 && #self.SeekSpawns > 0 then
		MySpawns = self.SeekSpawns
	else
		MySpawns = self.Spawns
	end

	-- Pick one of the spawn points.
	local ChosenPoint = nil
	
	local Count = table.Count( MySpawns )
	if Count == 0 then
		Msg("[PlayerSelectSpawn] Error! No spawn points!\n")
		return nil
	end
	
	for i = 1, 6 do
		ChosenPoint = MySpawns[ math.random( 1, Count ) ]
		
		if ChosenPoint && ChosenPoint:IsValid() && ChosenPoint:IsInWorld() then
			return ChosenPoint
		end
	end
	
	return ChosenPoint
end

function GM:PlayerShouldTakeDamage( victim, ply )
	-- Turn off friendly fire. It seems abit silly to tag another seeker or hiders killing eachother... ? :P
	if victim:IsPlayer() && ply:IsPlayer() then
		if victim:Team() == ply:Team() then
			return false
		end
	end
	
	return true
end

// Auto-heal players all the time
local function HealPlayers()
	for k,ply in pairs(player.GetAll()) do
		
		local MAXHEALTH = 0
		local HP = ply:Health()
		
		if ( ply:Team() == TEAM_SEEK ) then
			MAXHEALTH = MAX_SEEK_HEALTH
		elseif ( ply:Team() == TEAM_HIDE ) then
			MAXHEALTH = MAX_HIDE_HEALTH
		end
		
		if (ply:Alive() && !ply:Team() == TEAM_SPEC && HP < MAXHEALTH) then
			ply:SetHealth( HP + 1 )
		end
		
	end
end
timer.Create("HealPlayers", 0.2, 0, function() HealPlayers(); end)

-- Prevent suicide.
function GM:CanPlayerSuicide( ply )
	ply:PrintMessage( HUD_PRINTNOTIFY, "Your suicide has been logged!" )
	print(ply:Name() .. " suicided" )
	return true
end 


